<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单介绍" prop="orderTitle">
      <el-input v-model="dataForm.orderTitle" placeholder="订单介绍"></el-input>
    </el-form-item>
    <el-form-item label="关联id" prop="relevanceId">
      <el-input v-model="dataForm.relevanceId" placeholder="关联id"></el-input>
    </el-form-item>
    <el-form-item label="关联类型 1-新房 2-二手房 3-出租房" prop="relevanceType">
      <el-input v-model="dataForm.relevanceType" placeholder="关联类型 1-新房 2-二手房 3-出租房"></el-input>
    </el-form-item>
    <el-form-item label="经纪人ID" prop="brokerId">
      <el-input v-model="dataForm.brokerId" placeholder="经纪人ID"></el-input>
    </el-form-item>
    <el-form-item label="订单类型 0-未完成 1-已完成" prop="orderType">
      <el-input v-model="dataForm.orderType" placeholder="订单类型 0-未完成 1-已完成"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          orderTitle: '',
          relevanceId: '',
          relevanceType: '',
          brokerId: '',
          orderType: ''
        },
        dataRule: {
          orderTitle: [
            { required: true, message: '订单介绍不能为空', trigger: 'blur' }
          ],
          relevanceId: [
            { required: true, message: '关联id不能为空', trigger: 'blur' }
          ],
          relevanceType: [
            { required: true, message: '关联类型 1-新房 2-二手房 3-出租房不能为空', trigger: 'blur' }
          ],
          brokerId: [
            { required: true, message: '经纪人ID不能为空', trigger: 'blur' }
          ],
          orderType: [
            { required: true, message: '订单类型 0-未完成 1-已完成不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/order/order/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderTitle = data.order.orderTitle
                this.dataForm.relevanceId = data.order.relevanceId
                this.dataForm.relevanceType = data.order.relevanceType
                this.dataForm.brokerId = data.order.brokerId
                this.dataForm.orderType = data.order.orderType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/order/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'orderTitle': this.dataForm.orderTitle,
                'relevanceId': this.dataForm.relevanceId,
                'relevanceType': this.dataForm.relevanceType,
                'brokerId': this.dataForm.brokerId,
                'orderType': this.dataForm.orderType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
